package com.wzx.core.aop;

import com.wzx.core.aop.invoke.AopInvocation;
import com.wzx.core.ioc.context.AbstractApplicationContext;
import com.wzx.core.ioc.context.BeanPostProcess;
import net.sf.cglib.proxy.Enhancer;

public class AopBeanPostProcess implements BeanPostProcess {

    private AbstractApplicationContext context;
    private AdviceClassRegister adviceClassRegister;

    public AopBeanPostProcess(AbstractApplicationContext context) {
        this.context = context;
        this.adviceClassRegister = new AdviceClassRegister(context);

    }

    @Override
    public Object after(String beanName, Object bean) {
        if (adviceClassRegister.isAopProxy(beanName, bean.getClass())) {
            Enhancer enhancer = new Enhancer();
            enhancer.setSuperclass(bean.getClass());
            enhancer.setCallback(new AopInvocation(bean, context, adviceClassRegister));
            return enhancer.create();
        }
        return bean;
    }
}
